package com.dd.duu.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dd.dc.base.domain.DPage;
import com.dd.dc.base.domain.DResult;
import com.dd.dc.entity.users.DLevelInfo;
import com.dd.duu.service.LevelClient;
import io.seata.spring.annotation.GlobalTransactional;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * 描述: 用户等级
 *
 * @author: yanglin
 * @Date: 2021-03-16-14:29
 * @Version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/level/c/")
@GlobalTransactional(rollbackFor = Exception.class) // 分布式事务seata
public class LevelController {

    @Autowired
    LevelClient levelClient;

    @ApiOperation(value = "等级分页")
    @PostMapping("/queryLevelList")
    public DResult<DPage<DLevelInfo>> queryLevelList(@RequestParam int page, @RequestParam int limit, @RequestParam String levelName){
        log.info("UserUi queryLevelList start ...");
        Page<DLevelInfo> pages = levelClient.queryLevelList(page, limit, levelName);
        DPage<DLevelInfo> levelPage = DPage.to(pages);
        return DResult.of(levelPage);
    }

    @Transactional
    @ApiOperation(value = "等级新增/更新")
    @PostMapping("saveOrUpdate")
    public DResult saveOrUpdate(@RequestBody DLevelInfo levelInfo){
        boolean flag = false;
        // 判断新增/更新
        if (Objects.isNull(levelInfo.getLevelId())) {
            // 新增权限
            Integer pId = levelClient.insert(levelInfo);
            if (pId > 0){
                flag = true;
            }
        }else{
            // 更新权限
            flag = levelClient.updateById(levelInfo);
        }
        return DResult.of(flag);
    }

    @Transactional
    @ApiOperation(value = "等级删除")
    @DeleteMapping("deleteLevel")
    public DResult deleteLevel(@RequestParam Integer levelId){
        boolean flag = levelClient.delete(levelId);
        return DResult.of(flag);
    }
}
